// Behavior Created by Liferose
#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;

using Bots.Grind;
using Buddy.Coroutines;
using CommonBehaviors.Actions;
using Honorbuddy.QuestBehaviorCore;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.POI;
using Styx.Pathing;
using Styx.CommonBot.Profiles;
using Styx.CommonBot.Routines;
using Styx.Helpers;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.World;
using Styx.WoWInternals.WoWObjects;

using Action = Styx.TreeSharp.Action;
#endregion


namespace Honorbuddy.Quest_Behaviors.LifeProject.SpecificQuests.Kalimdor.Azshara
{
    [CustomBehaviorFileName(@"LifeProject\14134-TheCaptainsLog")]
    public class TheCaptainsLog : QuestBehaviorBase
    {
        #region Constructor and Argument Processing
        public TheCaptainsLog(Dictionary<string, string> args)
            : base(args)
        {
            QBCLog.BehaviorLoggingContext = this;

            try
            {
                QuestId = 14134;
                QuestRequirementComplete = QuestCompleteRequirement.NotComplete;
                QuestRequirementInLog = QuestInLogRequirement.InLog;
                Waypoint1 = new WoWPoint(2657.031, -4765.175, 136.3608);
                Waypoint2 = new WoWPoint(2631.121, -4685.699, 143.7551);
                Waypoint3 = new WoWPoint(2721.964, -4862.099, 116.9158);
                Waypoint4 = new WoWPoint(2657.29, -4904.915, 119.8741);
                Waypoint5 = new WoWPoint(2716.86, -4991.775, 118.1055);
            }

            catch (Exception except)
            {
                // Maintenance problems occur for a number of reasons.  The primary two are...
                // * Changes were made to the behavior, and boundary conditions weren't properly tested.
                // * The Honorbuddy core was changed, and the behavior wasn't adjusted for the new changes.
                // In any case, we pinpoint the source of the problem area here, and hopefully it can be quickly
                // resolved.
                QBCLog.Exception(except);
                IsAttributeProblem = true;
            }
        }

        protected override void EvaluateUsage_DeprecatedAttributes(XElement xElement)
        {
        }

        protected override void EvaluateUsage_SemanticCoherency(XElement xElement)
        {
        }

        public int QuestId { get; private set; }
        public WoWPoint Waypoint1 { get; private set; }
        public WoWPoint Waypoint2 { get; private set; }
        public WoWPoint Waypoint3 { get; private set; }
        public WoWPoint Waypoint4 { get; private set; }
        public WoWPoint Waypoint5 { get; private set; }
        public QuestCompleteRequirement QuestRequirementComplete { get; private set; }
        public QuestInLogRequirement QuestRequirementInLog { get; private set; }
        #endregion

        #region Private and Convenience variables
        public LocalPlayer Me { get { return (StyxWoW.Me); } }
        #endregion

        public WoWGameObject Lumber
        {
            get
            {
                return ObjectManager.GetObjectsOfType<WoWGameObject>()
					.Where(u => u.Entry == 195361 && u.Distance < 200 && !Blacklist.Contains(u.Guid, BlacklistFlags.Interact))
                    .OrderBy(u => u.Distance).FirstOrDefault();
            }
        }

		private bool GatherLumber()
		{
			return
				Lua.GetReturnVal<bool>("if GetPetActionCooldown(1) == 0 then CastPetAction(1) return 1 else return nil end", 0);
		}

        public override void OnStart()
        {
            var isBehaviorShouldRun = OnStart_QuestBehaviorCore(string.Empty);

            if (isBehaviorShouldRun)
            {
                LevelBot.BehaviorFlags &=
                    ~(BehaviorFlags.Combat | BehaviorFlags.Vendor);
            }
        }

        protected override Composite CreateBehavior_CombatOnly()
        {
            return new PrioritySelector(
                new Decorator(context => Query.IsInVehicle(),
                    new ActionAlwaysSucceed())
                );
        }

        protected override Composite CreateMainBehavior()
        {
            return new PrioritySelector(
                new Decorator(context => Me.IsQuestComplete(QuestId) || !Query.IsInVehicle(),
                    new Action(context => { BehaviorDone(); })),
                new Decorator(context => Lumber != null,
					new PrioritySelector(
						new Decorator(context => !Lumber.WithinInteractRange,
							new Sequence(
								new Action(context => { Navigator.MoveTo(Lumber.Location); }),
								new Action(context => { WoWMovement.MoveStop(); }),
								new WaitContinue(TimeSpan.FromMilliseconds(2500), context => false, new ActionAlwaysSucceed())
								)
							),
						new Decorator(context => Lumber.WithinInteractRange && !SpellManager.GlobalCooldown,
							new Sequence(
								new Action(context => GatherLumber() ? RunStatus.Success : RunStatus.Failure),
								new WaitContinue(TimeSpan.FromMilliseconds(2000), context => false, new ActionAlwaysSucceed())
								)
							),
						new Decorator(context => !Me.IsCasting,
							new Action(context => { Blacklist.Add(Lumber.Guid, BlacklistFlags.Interact, TimeSpan.FromSeconds(3 * 60)); })
							),
						new Decorator(context => Lumber == null,
							new Sequence(
								new Action(context => { Navigator.MoveTo(Waypoint1); }),
								new Action(context => { Navigator.MoveTo(Waypoint2); }),
								new Action(context => { Navigator.MoveTo(Waypoint3); }),
								new Action(context => { Navigator.MoveTo(Waypoint4); }),
								new Action(context => { Navigator.MoveTo(Waypoint5); })
								)
							)					
						)
                    )
                );
        }
    }
}
